<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	重置记录
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'网点业绩'),
	array('op'=>'listday','title'=>'每日充值统计'),
	array('op'=>'listmonth','title'=>'每月充值统计'),
);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$orderby='id desc';
	$list=pdo_getslice('jy_mapfuwu_store',$params,array($pindex,$psize),$total,array(),'',$orderby);
	if($list){
		$setting=$this->plugin_setting('setting');
		$fee_rate=$setting['fee_rate'];
		$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1);
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$params['store_id']=$item['id'];
			$item['totalprice']=pdo_getsum('jy_mapfuwu_order',$params,'price');
			$item['koufee']=$item['totalprice']*$fee_rate/100;
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'total'=>$total,'psize'=>$psize,'list'=>array(
			array('name'=>'member','title'=>'用户','type'=>'member'),
			array('name'=>'title','title'=>'网点名称','type'=>'string'),
			array('name'=>'totalprice','title'=>'总业绩','type'=>'string'),
			array('name'=>'koufee','title'=>'预计扣佣','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string','width'=>15),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='listday'){
	$setting=$this->plugin_setting('setting');
	$fee_rate=$setting['fee_rate'];
	$params=array('ispay'=>1,'uniacid'=>$_SESSION['uniacid']);
	$y=$_RQ['y']?:date('Y');
	$m=$_RQ['m']?:date('m');
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	if(!empty($_RQ['m'])){
		$return['params']['m']=$_RQ['m'];
	}
	$daynum=date("t",strtotime($y.'-'.$m));
	$list=array();
	for($i=1;$i<=$daynum;$i++){
		$starttime=strtotime($y.'-'.$m.'-'.$i);
		$endtime=$starttime+24*3600;
		$params['createtime between']=array($starttime,$endtime);
		$totalprice=pdo_getsum('jy_mapfuwu_order',$params,'price');
		$koufee=$totalprice*$fee_rate/100;
		$list[]=array(
			'title'=>$y.'-'.$m.'-'.$i,
			'totalprice'=>$totalprice,
			'koufee'=>$koufee,
		);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'日期','type'=>'string'),
			array('name'=>'totalprice','title'=>'总业绩','type'=>'string'),
			array('name'=>'koufee','title'=>'预计扣佣','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
			array('name'=>'m','width'=>15,'title'=>'月份','type'=>'radio','options'=>'months'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'1'=>'1月',
		'2'=>'2月',
		'3'=>'3月',
		'4'=>'4月',
		'5'=>'5月',
		'6'=>'6月',
		'7'=>'7月',
		'8'=>'8月',
		'9'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='listmonth'){
	$params=array('ispay'=>1,'uniacid'=>$_SESSION['uniacid']);
	$y=$_RQ['y']?:date('Y');
	if(!empty($_RQ['y'])){
		$return['params']['y']=$_RQ['y'];
	}
	$list=array();
	for($i=1;$i<=12;$i++){
		$starttime=strtotime($y.'-'.$i);
		if($i==12){
			$ny=$y+1;
			$endtime=strtotime($ny.'-01');
		}else{
			$m=$i+1;
			$endtime=strtotime($y.'-'.$m);
		}
		$params['createtime between']=array($starttime,$endtime);
		$totalprice=pdo_getsum('jy_mapfuwu_order',$params,'price');
		$koufee=$totalprice*$fee_rate/100;
		$list[]=array(
			'title'=>$y.'-'.$i,
			'totalprice'=>$totalprice,
			'koufee'=>$koufee,
		);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'title','title'=>'月份','type'=>'string'),
			array('name'=>'totalprice','title'=>'总业绩','type'=>'string'),
			array('name'=>'koufee','title'=>'预计扣佣','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('jy_mapfuwu_store',$params);
	if($list){
		$setting=$this->plugin_setting('setting');
		$fee_rate=$setting['fee_rate'];
		$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1);
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$params['store_id']=$item['id'];
			$item['totalprice']=pdo_getsum('jy_mapfuwu_order',$params,'price');
			$item['koufee']=$item['totalprice']*$fee_rate/100;
		}
	}
	$header=array(
		'mid'=>'ID',
		'title'=>'网点名称',
		'totalprice'=>'总业绩',
		'koufee'=>'预计扣佣',
	);
	cfc('tools')->export2excel($header,$list,'网点业绩');
}